const path = require("path")
const multer = require("koa-multer")
// const storage = multer.memoryStorage() //内存存储引擎将文件作为缓冲对象存储在内存中，它没有任何选项
// const upload = multer({ dest: './uploads/' }) //最简单方式

//重命名
const filename = (req, file, cb) => {
  const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
  cb(null, uniqueSuffix + path.extname(file.originalname))
}

//存储路径
const destination = (dirname) => {
  return (req, file, cb) => {
    cb(null, dirname)
  }
}

const storage = (dirname) => {
  return multer.diskStorage({
    destination: destination(dirname), 
    filename
  })
}

//图片集
const storagePhoto = storage(path.resolve(__dirname,"../../static/uploads"))
const photo = multer({storage:storagePhoto});

//头像
const storageAvatar = storage(path.resolve(__dirname,"../../static/avatar"))
const avatar = multer({storage:storageAvatar});


//通过内存存储
const storageMemory = multer.memoryStorage()
const memory = multer({storage:storageMemory})

module.exports = {
  photo,
  avatar,
  memory
}